package com.sanploy.fed.push.controller;

import com.google.gson.Gson;
import com.sanploy.fed.push.bean.ZkyUser;
import com.sanploy.fed.push.resp.BaseRspe;
import com.sanploy.fed.push.service.UserService;
import com.sanploy.fed.push.util.DateUtil;
import com.sanploy.fed.push.util.MD5;
import com.sanploy.fed.push.util.RespUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2017/4/7.
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    Gson gson = new Gson();

    //进行登录操作 // TODO: 2017/4/13 生产环境MD5加密
    @RequestMapping(value = "/user/login",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String doUserLogin(String name, String pwd){
        BaseRspe baseRspe = new BaseRspe();
        String resp ="";
        try {
            if(!"".equals(name) && !"".equals(pwd)){
                ZkyUser user  = userService.judgeLogin(name,pwd);
                if(user!=null){
                    String token = tokenGenerater(pwd);
                    userService.updateUserToken(token,user.getId()); //为验证通过的用户增加token
                    baseRspe.setCode(1);
                    baseRspe.setMsg(token);
                    baseRspe.setResponse_time(DateUtil.getCurrentTimestamp());
                }else {
                    RespUtil.baserespFail(baseRspe);
                }
            }else {
                RespUtil.baserespFail(baseRspe);
            }

        }catch (Exception e){
            System.out.println(e.toString());
            RespUtil.baserespFail(baseRspe);
        }finally {
            resp = gson.toJson(baseRspe);
        }

        return resp;
    }

    //进行注销操作
    @RequestMapping(value = "/user/logout",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public String doLogOut(@RequestParam String token){
        BaseRspe baseRspe = new BaseRspe();
        String resp ="";
        try {
            if(!"".equals(token) ){
                ZkyUser user = userService.judgeToken(token);
                if(user!=null){
                    userService.updateUserToken("",user.getId());
                    RespUtil.baserespSuccessful(baseRspe);
                }else {
                    RespUtil.baserespFail(baseRspe);
                }
            }else {
                RespUtil.baserespFail(baseRspe);
            }
        }catch (Exception e){
            System.out.println(e.toString());
            RespUtil.baserespFail(baseRspe);
        }finally {
            resp = gson.toJson(baseRspe);
        }

        return resp;
    }

    //查询登录状态
    @RequestMapping(value = "/user/status",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public String judgeStatus(@RequestParam String token){
        BaseRspe baseRspe = new BaseRspe();
        String resp ="";
        try {
            if(!"".equals(token)){
                ZkyUser user  = userService.judgeToken(token);
                if(user!=null){
                    baseRspe.setCode(1);
                    baseRspe.setMsg(user.getNickName());
                    baseRspe.setResponse_time(DateUtil.getCurrentTimestamp());
                }else {
                    RespUtil.baserespFail(baseRspe);
                }
            }else {
                RespUtil.baserespFail(baseRspe);
            }
        }catch (Exception e){
            System.out.println(e.toString());
            RespUtil.baserespFail(baseRspe);
        }finally {
            resp = gson.toJson(baseRspe);
        }

        return resp;
    }

    //预留的注册即接口
    @RequestMapping(value = "/user/reg",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String doReg(String name, String pwd, String nickName){
        BaseRspe baseRspe = new BaseRspe();
        String resp ="";
        try {
            if(!"".equals(name) && !"".equals(pwd) && !"".equals(nickName)){
                  int flag = userService.insertUser(name,pwd,nickName);
                  if(flag == 1){
                      RespUtil.baserespSuccessful(baseRspe);
                  }else {
                      RespUtil.baserespFail(baseRspe);
                  }
            }else {
                RespUtil.baserespFail(baseRspe);
            }
        }catch (Exception e){
            System.out.println(e.toString());
            RespUtil.baserespFail(baseRspe);
        }finally {
            resp = gson.toJson(baseRspe);
        }

        return resp;
    }

    //token生成
    private String tokenGenerater(String pwd){
        String token = "";
        if(!"".equals(pwd) && pwd!=null){
            token  = MD5.GetMD5Code(pwd+ DateUtil.getCurrentTimestamp()).substring(2,12); //生成登录时的Token
        }
        return token;
    }
}
